package com.xh.cdp.sys.dao.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.stereotype.Repository;

import com.xh.cdp.common.BaseDao;
import com.xh.cdp.sys.dao.SysRoleMenuDao;
import com.xh.cdp.sys.entity.SysRoleEntity;
import com.xh.cdp.sys.entity.SysRoleMenuEntity;

@Repository
public class SysRoleMenuDaoImpl extends BaseDao<SysRoleMenuEntity, String>
		implements SysRoleMenuDao {

	@Override
	public void addRoleMenu(String rolePower, Set<String> fatherMenuIds,
			String roleId) {
		Set<String> resultSet = new HashSet<>();
		String[] menuIds = rolePower.split(",");
		for (int i = 0; i < menuIds.length; i++) {
			resultSet.add(menuIds[i]);
		}
		for (String fatherMenuId : fatherMenuIds) {
			resultSet.add(fatherMenuId);
		}
		for (String result : resultSet) {
			save(new SysRoleMenuEntity(roleId, result));
		}
	}

	@Override
	public List<String> getOwnMenuIds(String id) {
		return listSinglePropertyByHql(String.class,
				"SELECT sysMenuId FROM SysRoleMenuEntity WHERE sysRoleId = ?",
				id);
	}

	/**
	 * 该方法非常危险！！！
	 */
	@Override
	public void deleteRoleMenuInPhysical(SysRoleEntity role) {
		List<SysRoleMenuEntity> list = listEntityByHql(
				"FROM SysRoleMenuEntity WHERE sysRoleId = ?", role.getId());
		for (int i = 0; i < list.size(); i++) {
			delete(list.get(i));
		}
	}

}
